package com.hsr.web.controller;


import com.hsr.web.dto.ArticleDTO;
import com.hsr.web.dto.ArticleQuery;
import com.hsr.web.entity.TArticle;
import com.hsr.web.service.ITArticleService;
import com.hsr.web.utils.ResultVOUtil;
import com.hsr.web.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tb
 * @since 2022-04-02
 */
@RestController
@Api(tags = "文章相关接口")
@RequestMapping("/article")
public class TArticleController {

    @Autowired
    ITArticleService itArticleService;

    @PostMapping("/list")
    @ApiOperation("查询")
    public ResultVO list(@RequestBody ArticleQuery query) {
        return ResultVOUtil.success(itArticleService.getList(query));
    }

    @GetMapping("/detail/{id}")
    @ApiOperation("详情")
    public ResultVO getOne(@PathVariable String id) {
        return ResultVOUtil.success(itArticleService.getById(id));
    }

    @PutMapping
    @ApiOperation("新增")
    public ResultVO insert(@RequestBody ArticleDTO articleDTO){
        TArticle article = new TArticle();
        BeanUtils.copyProperties(articleDTO,article);
        itArticleService.save(article);
        return ResultVOUtil.success();
    }

    @PutMapping("/{id}")
    @ApiOperation("编辑")
    public ResultVO update(@RequestBody ArticleDTO articleDTO,@PathVariable String id){
        articleDTO.setId(id);
        TArticle article = new TArticle();
        BeanUtils.copyProperties(articleDTO,article);
        itArticleService.updateById(article);
        return ResultVOUtil.success();
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除")
    public ResultVO delete(@PathVariable String id){
        itArticleService.removeById(id);
        return ResultVOUtil.success();
    }


}
